import 'basic_types.dart';

/// Message websocket通信消息结构体
class Message {
  String? exchangeId;
  NodeId? srcNode;
  NodeId? dstNode;
  MethodType method;
  dynamic data;

  Message({required this.method, required this.data});

  Message.fromJson(Map<String, dynamic> json)
      : method = MethodType.byName(json["method"] as String? ?? ""),
        exchangeId = json["exchangeId"] as String?,
        srcNode = json["srcNode"] as NodeId?,
        dstNode = json["dstNode"] as NodeId?,
        data = json["data"];

  Map<String, dynamic> toJson() {
    var json = {
      "method": method.name,
      "data": data,
    };
    if (exchangeId != null && exchangeId!.isNotEmpty) {
      json["exchangeId"] = exchangeId;
    }
    if (srcNode != null && srcNode!.isNotEmpty) {
      json["srcNode"] = srcNode;
    }
    if (dstNode != null && dstNode!.isNotEmpty) {
      json["dstNode"] = dstNode;
    }
    return json;
  }
}

/// AttributePathIB 属性路径
class AttributePathIB {
  EndpointId endpoint;
  ClusterId cluster;
  AttributeId attribute;

  AttributePathIB({required this.endpoint, required this.cluster, required this.attribute});

  AttributePathIB.fromJson(Map<String, dynamic> json)
      : endpoint = json["endpoint"] as EndpointId,
        cluster = json["cluster"] as ClusterId,
        attribute = json["attribute"] as AttributeId;

  Map<String, dynamic> toJson() => {
        "endpoint": endpoint,
        "cluster": cluster,
        "attribute": attribute,
      };
}

/// AttributeDataIB
class AttributeDataIB {
  AttributePathIB path;
  StatusCode? status;
  dynamic data;
  int? dataVersion;

  AttributeDataIB({required this.path, required this.data});

  AttributeDataIB.fromJson(Map<String, dynamic> json)
      : path = AttributePathIB.fromJson(json["path"] as Map<String, dynamic>),
        data = json["data"],
        dataVersion = json["dataVersion"] as int? {
    var st = json["status"] as int?;
    if (st != null) {
      status = StatusCode.byValue(st);
    }
  }

  Map<String, dynamic> toJson() {
    var json = {
      "path": path.toJson(),
      "data": data,
    };
    if (status != null) {
      json["status"] = status!.value;
    }
    if (dataVersion != null) {
      json["dataVersion"] = dataVersion;
    }
    return json;
  }
}
